import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store/index';
import {getUserinfo} from '@/api';

import 'mint-ui/lib/style.css';
import 'vant/lib/index.css';
import './assets/style.css';


import 'video.js/dist/video-js.css';
import './assets/app.css';
import {getCookie, parseUrl} from './utils/function';



import { Search, Tab, Tabs, Swipe, SwipeItem, DropdownMenu, DropdownItem, Switch, Toast, Picker, DatetimePicker, Area, Checkbox, CheckboxGroup, ImagePreview, CountDown, SubmitBar, Popup, List, Lazyload, Field } from 'vant';
//import {Toast} from 'vant';

Vue.use(Toast).use(Picker).use(DatetimePicker).use(Area).use(Checkbox).use(CheckboxGroup).use(ImagePreview).use(CountDown).use(Popup).use(Field);
Vue.use(Search).use(Tab).use(Tabs).use(Swipe).use(SwipeItem).use(DropdownMenu).use(DropdownItem).use(Switch).use(SubmitBar).use(List).use(Lazyload);
//Toast('提示内容');

Vue.config.productionTip = false;
Vue.config.devtools = true;
Vue.config.silent = true;
Vue.prototype.$toast = Toast;

Vue.directive('title', {
  inserted: function (el, binding) {
    document.title = el.dataset.title;
  }
});


let params = parseUrl();
store.dispatch('setParams', params);


// 路由拦截
router.beforeEach((to, from, next) => {
  let localStorageUserInfo = JSON.parse(localStorage.getItem('userInfo'));
  if(store.state.userinfo && store.state.userinfo.nickname) return next();

  if(localStorageUserInfo && localStorageUserInfo.nickname) {
    store.dispatch('setUserinfo', localStorageUserInfo);
    return next()
  };

  getUserinfo().then((response)=>{
    if(response.code === 0){
      store.dispatch('setUserinfo', response.data.userinfo);
      localStorage.setItem('userInfo', JSON.stringify(response.data.userinfo));
      console.log('用户信息', store.state.userinfo)
      next();
    }
  });

});



new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
